//获取应用实例
var util = require('../../utils/util.js')
var app = getApp();
var md5 = require('../md5.js');
Page({
  data: {
    listAll: [{
        id: '1',
        image: "../../images/2.jpg",
        title: "构建人工智能时代的学习型城市圆桌会",
        location: "国家会展中心北极星",
        time: "2010/01/18   18:00",
        ellipsis: true,
      },
      {
        id: '2',
        image: "../../images/5.jpg",
        title: "构建人工智能时代的学习型城市圆桌会",
        location: "国家会展中心北极星",
        time: "2010/01/18   18:00",
        ellipsis: true,
      },
      {
        id: '3',
        image: "../../images/2.jpg",
        title: "构建人工智能时代的学习型城市圆桌会",
        location: "国家会展中心北极星",
        time: "2010/01/18   18:00",
        ellipsis: true,
      },
      {
        id: '3',
        image: "../../images/2.jpg",
        title: "构建人工智能时代的学习型城市圆桌会",
        location: "国家会展中心北极星",
        time: "2010/01/18   18:00",
        ellipsis: true,
      },
      {
        id: '3',
        image: "../../images/2.jpg",
        title: "构建人工智能时代的学习型城市圆桌会",
        location: "国家会展中心北极星",
        time: "2010/01/18   18:00",
        ellipsis: true,
      },
      {
        id: '3',
        image: "../../images/2.jpg",
        title: "构建人工智能时代的学习型城市圆桌会",
        location: "国家会展中心北极星",
        time: "2010/01/18   18:00",
        ellipsis: true,
      },
    ],
    ellipsis: true, // 文字是否收起，默认收起
  },
  //底部导航跳转
  toAiIndex: function () {
    util.toAiIndex()
  },
  toContactsIng: function () {
    util.toContactsIng()
  },
  toProductRevision: function () {
    util.toProductRevision()
  },
  toMy: function () {
    util.toMy()
  },
  toBadge: function () {
    util.toBadge()
  },
  onPullDownRefresh: function(){
    wx.stopPullDownRefresh({
      complete: (res) => {},
    });
  },
  onLoad: function () {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'Mine'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '我的'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    //exhibitionid='33'
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/user/myexhibition'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u

      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          for (var i = 0; i < curdata.length; i++) {
            curdata[i].ellipsis=false
          }
          that.setData({
            myExpo: curdata
          });
        }, fail: res => {
          console.log(res, "error")
        }

      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  qhExhibition: function (e) {
    var exhibitionid = e.currentTarget.dataset.id
    console.log('exhibitionid=' + exhibitionid)
    wx.showModal({
      title: '确定切换成当前展会吗',
      content: '',
      success(res) {
        if (res.confirm) {
          var u = wx.getStorageSync('u')
          wx.setStorageSync('exhibitionid', exhibitionid);
          //默认最新（调用pico接口）
          var timestamp = Date.parse(new Date());
          timestamp = timestamp / 1000;
          var url = app.globalData.picoaddress + '/attendee/user/myexhibition'
          var k = {}
          k["v"] = '1.0.0'
          k["client"] = 'ios'
          k["timestamp"] = timestamp
          k["lan"] = 'cn'
          k["exhibitionid"] = exhibitionid
          k["u"] = u
          var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
          console.log(sign)
          var curb64 = md5.hexMD5(sign)
          console.log(curb64)
          k["sign"] = curb64
          wx.request({
            header: {
              'content-type': 'application/x-www-form-urlencoded',
            },
            url: url,
            method: 'post',
            data: k,
            success(res) {
              console.log(res)
              var mesg = res.data
              console.log('mesg=' + mesg)
              var curdata = mesg.data
              for (var i in curdata){
                var curexhibitionid = curdata[i].exhibitionid
                if (exhibitionid == curexhibitionid){
                  var curu = curdata[i].u
                  console.log('curu=' + curu)
                  wx.setStorageSync('u', curu);
                }
              }
              wx.reLaunch({
                url: '../aiIndex/aiIndex'
              });
              
            }, fail: res => {
              console.log(res, "error")
            }

          })
          console.log('用户点确定了')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      } 
     })

  },
  /**
   * 收起/展开按钮点击事件
   */
  ellipsis: function (e) {
    var item = e.currentTarget.dataset.item || {};
    var myExpo = this.data.myExpo || [];
    for (var index in myExpo) {
      var article = myExpo[index];
      if (article.exhibitionid == item.exhibitionid) {
        article.ellipsis = !article.ellipsis
      }
    }
    this.setData({
      myExpo: myExpo
    })
  },
})